package com.caipos.mqtt.config;

import com.caipos.mqtt.handler.CaiposMessageHandler;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

/**
 * @Company caipos
 * @Author weihengXu QQ:1280058248
 */
@Slf4j
@Configuration
public class MqttConfiguration {

	@Autowired
	private MqttProperties mqttProperties;

	@Value("${spring.mqtt.default-topic}")
	private String topic;

	@Autowired
	private CaiposMessageHandler caiposMessageHandler;

	@Bean
	public MqttConnectOptions getMqttConnectOptions() {
		MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
		mqttConnectOptions.setUserName(mqttProperties.getUsername());
		mqttConnectOptions.setPassword(mqttProperties.getPassword().toCharArray());
		mqttConnectOptions.setServerURIs(new String[] { mqttProperties.getHostUrl() });
//		mqttConnectOptions.setKeepAliveInterval(1);
//		mqttConnectOptions.setConnectionTimeout(1);
		return mqttConnectOptions;
	}

	@Bean
	public MqttPahoClientFactory mqttClientFactory() {
		DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
		factory.setConnectionOptions(getMqttConnectOptions());
		return factory;
	}

	@Bean
	@ServiceActivator(inputChannel = "mqttOutboundChannel")
	public MessageHandler mqttOutbound() {
		MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(UUID.randomUUID().toString()+"_producer",
				mqttClientFactory());
		messageHandler.setAsync(true);
		messageHandler.setDefaultTopic(mqttProperties.getDefaultTopic());
		return messageHandler;
	}

	@Bean
	public MessageChannel mqttOutboundChannel() {
		return new DirectChannel();
	}

	// ---------------------------------订阅--------------------------------------
	@Bean
	public MessageChannel mqttInputChannel() {
		return new DirectChannel();
	}

	@Bean
	public MessageProducer inbound() throws Exception {
		MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(
				mqttProperties.getClientId()+ System.currentTimeMillis()+"_consumer", mqttClientFactory(), topic,"ceshi/#");
		adapter.setCompletionTimeout(1000);
		adapter.setConverter(new DefaultPahoMessageConverter());
		adapter.setQos(1);
		adapter.setOutputChannel(mqttInputChannel());
		return adapter;
	}

	@Bean
	@Transactional
	@ServiceActivator(inputChannel = "mqttInputChannel")
	public MessageHandler handler() {
		return caiposMessageHandler;
	}


}
